﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;



namespace lottery
{

    public partial class Form1 : Form
    {
        public ArrayList datas = new ArrayList();
        //delegate void ps();
        Boolean isPause = false;
        //按下空格抽奖次数
        int spaceCount = 0;

        string cfgFile = Application.StartupPath + @"\config.ini";
        string bakcGroundPictrue = "";
        //抽奖类型，1 随机定时抽，2 手动抽（按空格键） 
        int LotteryType = 0;
        //待选滚动方式 1 号码池随机滚动 2 号码池顺序滚动
        int rollType = 0;
        //已中奖号码从待选池中去除
        string exceptMode = "";
        //作弊号码，即必定会中奖的号码（多号码用逗号隔开，注意多个中奖号码时中奖顺序为1,3,5,7,9,11，即奇数顺序）
        ArrayList cheatNumber;
        //高中奖率号码，提高20%中奖率，多号码逗号隔开
        ArrayList HighRateNumber;

        //数据文件地址
        string numberDataFile = "";
        //抽奖框位置
        int numberBoxLocationLeft = 0;
        int numberBoxLocationTop = 0;
        int numberBoxLocationWidth = 0;
        int numberBoxLocationHeight = 0;
        int numberBoxFontSize = 36;

        //中奖框位置
        int luckyNumberBoxLocationLeft = 0;
        int luckyNumberBoxLocationTop = 0;
        int luckyNumberBoxLocationWidth = 0;
        int luckyNumberBoxLocationHeight = 0;
        int luckyNumberBoxFontSize = 36;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CheckCode ck = new CheckCode();

            if (ck.checkCode()) {
                label1.Text = ck.corpName;
            }

            IniCtl ini = new IniCtl(cfgFile);
            bakcGroundPictrue = ini.get("bakcGroundPictrue");
            //抽奖类型，1 随机定时抽，2 手动抽（按空格键） 
            LotteryType = Convert.ToInt32("0" + ini.get("LotteryType"));
            //待选滚动方式 1 号码池随机滚动 2 号码池顺序滚动
            rollType = Convert.ToInt32("0" + ini.get("rollType"));
            //已中奖号码从待选池中去除
            exceptMode = ini.get("exceptMode");
            //作弊号码，即必定会中奖的号码（多号码用逗号隔开，注意多个中奖号码时中奖顺序为1,3,5,7,9,11，即奇数顺序）
            //cheatNumber = new ArrayList(ini.get("cheatNumber").Split(','));
            cheatNumber = new ArrayList();
            //高中奖率号码，提高20%中奖率，多号码逗号隔开
            //HighRateNumber = new ArrayList(ini.get("HighRateNumber").Split(','));
            HighRateNumber = new ArrayList();

            //数据文件地址
            numberDataFile = ini.get("numberDataFile");
            //抽奖框位置
            numberBoxLocationLeft = Convert.ToInt32("0" + ini.get("numberBoxLocationLeft"));
            numberBoxLocationTop = Convert.ToInt32("0" + ini.get("numberBoxLocationTop"));
            numberBoxLocationWidth = Convert.ToInt32("0" + ini.get("numberBoxLocationWidth"));
            numberBoxLocationHeight = Convert.ToInt32("0" + ini.get("numberBoxLocationHeight"));
            numberBoxFontSize = Convert.ToInt32("0" + ini.get("numberBoxFontSize"));

            //中奖框位置
            luckyNumberBoxLocationLeft = Convert.ToInt32("0" + ini.get("luckyNumberBoxLocationLeft"));
            luckyNumberBoxLocationTop = Convert.ToInt32("0" + ini.get("luckyNumberBoxLocationTop"));
            luckyNumberBoxLocationWidth = Convert.ToInt32("0" + ini.get("luckyNumberBoxLocationWidth"));
            luckyNumberBoxLocationHeight = Convert.ToInt32("0" + ini.get("luckyNumberBoxLocationHeight"));
            luckyNumberBoxFontSize = Convert.ToInt32("0" + ini.get("luckyNumberBoxFontSize"));
            if (File.Exists(numberDataFile))
            {

                StreamReader sr = new StreamReader(numberDataFile, Encoding.Default); // 使用默认编码

                while (!sr.EndOfStream)//判断是否读完文件，EndOfStream表示是否是流文件的结尾
                {
                    string str = sr.ReadLine();
                    if (str != "")
                    {
                        datas.Add(str);
                    }

                    // 按照行读取
                }
            }
            //插入10倍中奖率号码
            if (HighRateNumber.Count > 0)
            {
                foreach (string n in HighRateNumber)
                {
                    if (datas.IndexOf(n) > -1)
                    {
                        //for (int i = 0; i < Math.Round((9.0* datas.Count/(datas.Count-10)),0); i++)
                        for (int i = 0; i < 9; i++)
                        {
                            datas.Add(n);
                        }
                    }
                }
            }


            this.Location = new Point(0, 0);
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            pictureBox1.Location = new Point(0, 0);
            pictureBox1.Width = this.Width;
            pictureBox1.Height = this.Height;
            pictureBox1.Image = Image.FromFile(bakcGroundPictrue);
            label1.Parent = pictureBox1;
            textBox1.Width = numberBoxLocationWidth == 0 ? 400 : numberBoxLocationWidth;
            textBox1.Height= numberBoxLocationHeight==0?50: numberBoxLocationHeight;
            textBox1.Font= new Font("微软雅黑",numberBoxFontSize ==0?36: numberBoxFontSize);

            int tLeft = numberBoxLocationLeft == 0 ? this.Width / 2 - textBox1.Width / 2 : numberBoxLocationLeft;
            int tTop = numberBoxLocationTop == 0 ? this.Height / 2 - textBox1.Width / 2 : numberBoxLocationTop;

            textBox1.Location = new Point(tLeft, tTop);
            textBox1.ReadOnly = true;

            textBox2.Width = luckyNumberBoxLocationWidth == 0 ? 400 : luckyNumberBoxLocationWidth;
            textBox2.Height = luckyNumberBoxLocationHeight == 0 ? 800 : luckyNumberBoxLocationHeight;
            textBox2.Font = new Font("微软雅黑", luckyNumberBoxFontSize == 0 ? 36 : luckyNumberBoxFontSize);

            int luckyBoxLeft = numberBoxLocationLeft == 0 ? this.Width -450: numberBoxLocationLeft;
            int luckyBoxTop = numberBoxLocationTop == 0 ? 40 : numberBoxLocationTop;

            textBox2.Location = new Point(luckyBoxLeft, luckyBoxTop);
            textBox2.Visible = false;


            timer1.Interval = 10;

            timer1.Start();

        }

        public void changeNumber()
        {
            if (datas.Count == 0) { return; }
            int numberPool = datas.Count;
            Random rd = new Random();
            textBox1.Text = datas[(int)Math.Floor(Convert.ToDouble(numberPool) * rd.NextDouble())].ToString();
        }

        private void altS(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Modifiers == Keys.Alt)
            {
                Form form2 = new Form2();
                form2.ShowDialog();
            }
            else if (e.KeyCode == Keys.Space)
            {
                isPause = !isPause;
                if (isPause)
                {
                    if(spaceCount==0) {
                        textBox2.Visible = true;
                    }
                    if (spaceCount % 2 == 0 && cheatNumber.Count>0 && cheatNumber[0].ToString()!="") {
                        //必中号码中奖
                        textBox1.Text = cheatNumber[0].ToString();
                        cheatNumber.RemoveAt(0);
                        spaceCount++;
                    }
                    if (datas.Count == 0)
                    {
                        return;
                    }
                    //是否中奖后移除模式
                    if (exceptMode.ToLower() == "on")
                    {
                       while (datas.IndexOf(textBox1.Text) > -1)
                        {   //清除掉与当前显示号码相同的所有号码，确保已中奖的高中奖率号码全部清除
                            datas.Remove(datas[datas.IndexOf(textBox1.Text)]);
                        }
                        //移除已中奖的必中号码
                        if (cheatNumber.IndexOf(textBox1.Text) > -1)
                        {
                            cheatNumber.Remove(cheatNumber[cheatNumber.IndexOf(textBox1.Text)]);
                        }
                        //移除已中奖的高中奖率号码
                        if (HighRateNumber.IndexOf(textBox1.Text) > -1)
                        {

                            HighRateNumber.Remove(HighRateNumber[HighRateNumber.IndexOf(textBox1.Text)]);
                        }
                        textBox2.Text += textBox1.Text + "\r\n";
                    }
                }

            }
            textBox2.Select(textBox2.TextLength, 0);
            textBox2.ScrollToCaret();
            textBox1.Focus();
        }

        private void paint(object sender, PaintEventArgs e)
        {
            PictureBox pb = sender as PictureBox;
            foreach (Control C in pb.Controls)
            {
                if (C is Label)
                {
                    Label L = (Label)C;
                    L.Visible = false;
                    e.Graphics.DrawString(L.Text, L.Font, new SolidBrush(L.ForeColor),
                        new RectangleF(L.Left - pb.Left, L.Top - pb.Top, L.Width, L.Height));
                }
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isPause == false)
            {
                changeNumber();
            }

        }
        
    }
}
